﻿<?php
class ProductsController extends AppController {
	var $name = 'Products';
	var $helpers = array('Html','Ck');
	var $components = array('Init', 'Param');

	function beforeFilter() {
		if (!$this->Session->read('admin')) {
			$this->redirect('/users/login');
		}
		else {
			$this->Init->startup($this);
			$this->Admin = $this->Init->init();

			$this->set('dashboard',0);
			$this->set('content',0);
			$this->set('products',1);
			$this->set('component',0);
			$this->set('system',0);

			$this->set('title','Product Management');
			$this->set('sub_title', $this->Param->getAction($this->params));
  		}
	}

	function index() {
		$this->loadModel('Product');
		$this->Product->loadType();
		$_param_pagination = array();
		if (!empty($_POST['sname']) && $_POST['sname'] != "Tên sản phẩm" ) {
			$condition['Product.name_eng like'] = '%'.trim(strip_tags($_POST['sname'])).'%';
			echo $_POST['sname'];
			$_param_pagination['sname'] = $_POST['sname'];
		}
		if (!empty($this->params['named']['sname']) && $this->params['named']['sname'] != "Tên sản phẩm" ) {
			$condition['Product.name_eng like'] = '%'.trim(strip_tags($this->params['named']['sname'])).'%';
			$_param_pagination['sname'] = $this->params['named']['sname'];
		}
		if (!empty($_POST['reason']) && $_POST['reason'] != 1 ) {
			$condition['Product.type'] = $_POST['reason'];
			$_param_pagination['reason'] = $_POST['reason'];
		}
		if (!empty($this->params['named']['reason']) && $this->params['named']['reason'] != 1 ) {
			$condition['Product.type'] = $this->params['named']['reason'];
			$_param_pagination['reason'] = $this->params['named']['reason'];
		}
		$condition['Product.status'] = 1;
		//print_r ($condition);
		$this->set('_param_pagination', $_param_pagination);
		$this->paginate = array(
			'Product' => array(
				'conditions' => $condition,
				'limit' => 20
			)
		);

		$this->set('products', $this->paginate('Product'));

		// category
		$this->loadModel('ProductType');
		$categories = $this->ProductType->getList();
		$this->set('group',$categories);
	}

	function edit($id = null){
		$this->loadModel('Product');
		$this->helpers[] = 'Ck';
		$id = $this->Param->getID($this->params);
		App::import('Vendor', 'SpawEditor', array('file' => 'editor/spaw.inc.php'));
		if (isset($this->data)) {
			$this->data['Product']['date'] = date('Y-m-d H:i:s');
			$this->data['Product']['name_eng'] = trim(strip_tags($this->data['Product']['name_eng']));
			$this->data['Product']['name_vie'] = trim(strip_tags($this->data['Product']['name_vie']));
			
			if(!empty($_FILES['upload']['name'])) {

				App::import('Component', 'Upload');
				$upload = new UploadComponent();
				$upload->setPath('products');
				//$_error = $upload->checkImage($_FILES['upload']['size']);

				//if(empty($_error)) {
					$_file = $upload->upload($_FILES['upload'], $id);
					$this->data['Product']['img'] = $_file;
				/*}
				else {
					$this->Session->setFlash($_error);
					$this->redirect('/products');
				}
				}
				*/
			}
			
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash('Done successfully!');
				$this->redirect('/products');
			}
			
		}
		else {
			if (isset($id) && ($id > 0)) {
				$this->data = $this->Product->find('first',array(
					'conditions' => array('Product.id' => $id)
				));
				$this->set('parent', $this->data['Product']['type']);
			}
			else {
				$this->set('parent', 0);
			}
		}

		$this->loadModel('ProductType');
		$categories = $this->ProductType->getList();
		$this->set('categories',$categories);
	}

	function del($id = null) {
		$id = $this->Param->getID($this->params);
		 $this->Product->id = $id;
		 $this->Product->delete();
		 $this->Session->setFlash('Done successfully.');
		 $this->redirect('/products');
	}
}
?>
